package cn.edu.zut.score.core.domain;

import cn.edu.zut.score.core.cache.RedisCache;
import cn.edu.zut.score.core.entity.User;
import cn.edu.zut.score.core.util.DateUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zby
 * @date 2021/06/24
 */
@Data
@ApiModel("实体操作记录")
public class DataLogInfo {

    @ApiModelProperty(value = "创建者")
    @TableField("create_user")
    private Long createUser;

    @ApiModelProperty(value = "创建时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private Date createTime;

    @ApiModelProperty(value = "修改者")
    @TableField("update_user")
    private Long updateUser;

    @ApiModelProperty(value = "修改时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("update_time")
    private Date updateTime;

    @ApiModelProperty(value = "逻辑删除,0未删除，1删除")
    @TableField("delete_flag")
    private Integer deleteFlag;

    @ApiOperation("填充create信息")
    public void fillCreateInfo(HttpServletRequest httpServletRequest, RedisCache redisCache) {
        String token = httpServletRequest.getHeader("Authorization");
        User currentUser = redisCache.getCurrentUser(token);
        Long operateId = currentUser.getId();
        Date now = DateUtils.getCurrDate();
        this.createTime = now;
        this.createUser = operateId;
        this.updateTime = now;
        this.updateUser = operateId;
    }

    @ApiOperation("填充update信息")
    public void fillUpdateInfo(HttpServletRequest httpServletRequest, RedisCache redisCache) {
        String token = httpServletRequest.getHeader("Authorization");
        User currentUser = redisCache.getCurrentUser(token);
        this.updateUser = currentUser.getId();
        this.updateTime = DateUtils.getCurrDate();
    }
}
